#-*- coding: utf-8 -*-
import codecs

def write(j, kanji, kana, trans):
    w = str(j) + "~#"
    i = 0
    write = codecs.open(r"/home/alvik/workspace/PraserDict/src/ww.txt", "a", 'utf-8')
    if len(kanji) <= 0:
        w = w + "~#"
    for e in kanji:
        w = w + e.encode("utf8")
        i = i + 1
        if i != len(kanji):
            w = w + ","
        else:
            w = w + "~#"
    i = 0
    if len(kana) <= 0:
        w = w + "~#"
    for e in kana:
        w = w + e.encode("utf8")
        i = i + 1
        if i != len(kana):
            w = w + ","
        else:
            w = w + "~#"
    i = 0
    if len(trans) <= 0:
        w = w + "\n"
    for e in trans:
        w = w + e.encode("utf8")
        i = i + 1
        if i != len(trans):
            w = w + ","
        else:
            w = w + "\n"
    write.write(w)
    write.close()

def prase():
    file = codecs.open(r"/home/alvik/workspace/PraserDict/src/JMdict_e", "r", 'utf-8')
    kanji = []
    kana = []
    trans = []
    j = 0;
    for line in file.readlines():
        if line.find("<keb>") != -1:
            i = line.find("</keb>")
            kanji.append(line[5:i])
        if line.find("<reb>") != -1:
            i = line.find("</reb>")
            kana.append(line[5:i])
        if line.find("<re_restr>") != -1:
            i = line.find("</re_restr>")
            e = kana.pop()
            e = e + ":" + line[10:i]
            kana.append(e)
        if line.find("<stagr>") != -1:
            i = line.find("</stagr>")
            trans.append("stagr:" + line[7:i])
        if line.find("<pos>") != -1:
            i = line.find("</pos>")
            if len(trans) > 0:
                e = trans.pop()
                e = e + ":pos:" + line[5:i]
                trans.append(e)
            else:
                trans.append("pos:" + line[5:i])    
        if line.find("<xref>") != -1:
            i = line.find("</xref>")
            if len(trans) > 0:
                e = trans.pop()
                e = e + ":xref:" + line[6:i]
                trans.append(e)
            else:
                trans.append("xref:" + line[6:i])   
        if line.find("<misc>") != -1:
            i = line.find("</misc>")
            if len(trans) > 0:
                e = trans.pop()
                e = e + ":misc:" + line[6:i]
                trans.append(e)
            else:
                trans.append("misc:" + line[6:i])  
        if line.find("<dial>") != -1:
            i = line.find("</dial>")
            if len(trans) > 0:
                e = trans.pop()
                e = e + ":dial:" + line[6:i]
                trans.append(e)
            else:
                trans.append("dial:" + line[6:i])
        if line.find("<gloss>") != -1:
            i = line.find("</gloss>")
            if len(trans) > 0:
                e = trans.pop()
                e = e + ":gloss:" + line[7:i]
                trans.append(e)
            else:
                trans.append("gloss:" + line[7:i])
        if line.find("</entry>") != -1:
            j = j + 1
            write(j, kanji, kana, trans)
            kanji = []
            kana = []
            trans = []
    file.close()


if __name__ == "__main__":    #(4) (5)
    prase()
